package com.gitee.l0km.thrift.swift2thrift.generator;

import com.gitee.l0km.codegen.annotations.CodegenLength;
import com.gitee.l0km.xthrift.base.metadata.DecoratorThriftFieldMetadata;
import com.gitee.l0km.xthrift.base.metadata.ThriftFieldMetadataUtil;
import com.google.common.base.Function;

public class CodegenErpcSizeLimitGetter implements Function<DecoratorThriftFieldMetadata, String> {

	public CodegenErpcSizeLimitGetter() {
	}

	@Override
	public String apply(DecoratorThriftFieldMetadata input) {
		 CodegenLength ann = ThriftFieldMetadataUtil.extractFieldAnnotation(input,CodegenLength.class);
		if(ann != null){
			if(ann.prealloc()){
				return "";
			}
			int limit;
			if(ann.value() > 0){
				limit = ann.value();
			}else if(ann.max() > 0){
				limit = ann.max();
			} else{
				return "";
			}
			if(String.class.equals(input.getErpcType().getJavaType())){
				limit++;
			}
			return "@max_length(" + limit + ")";
		}
		return "";
	}

}
